<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MyIssueController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $issues = \Auth::user()->issues();

        if ($request->query('replied')) {
            $issues->isReplied();
        }

        $issues = $issues->orderBy('created_at', 'desc')->get();

        $data = [];

        foreach ($issues as $issue) {
            $item = [
                'id'                => $issue->id,
                'name'              => $issue->goods->store->member->information->company,
                'publish_time'      => date('Y-m-d H:i:s', strtotime($issue->created_at)),
                'publish_timestamp' => strtotime($issue->created_at),
                'question'          => $issue->content,
                'replied'           => $issue->reply()->count(),
                'anonymity'         => $issue->anonymit ?: 0,
                'goods_id'          => $issue->goods_id,
            ];

            if ($item['replied']) {
                $item['reply'] = $issue->reply->content;
                $item['reply_time'] = date('Y-m-d H:i:s', strtotime($issue->reply->created_at));
                $item['reply_timestamp'] = strtotime($issue->reply->created_at);
            }

            $data[] = $item;
        }

        return success_return(['list' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
